import { BASE_URL, PAGE_SIZE } from './config.js'
const req = (url, method = 'GET', data = {}) => new Promise((resolve, reject) => {
  uni.request({
    url: BASE_URL + url,
    method,
    data,
    success: res => res.data.code === 200 ? resolve(res.data.data) : reject(res.data.msg),
    fail: reject
  })
})
// 批次
export const getBatchList = p => req('/batch', 'GET', { ...p, size: PAGE_SIZE })
export const addBatch     = d => req('/batch', 'POST', d)
export const delBatch     = id => req(`/batch/${id}`, 'DELETE')
// 队伍
export const getTeamList = p => req('/team', 'GET', { ...p, size: PAGE_SIZE })
export const addTeam     = d => req('/team', 'POST', d)
export const delTeam     = id => req(`/team/${id}`, 'DELETE')
// 材料&总结
export const getMaterialList = p => req('/material', 'GET', { ...p, size: PAGE_SIZE })
export const auditMaterial   = (id, pass) => req(`/material/${id}`, 'PUT', { pass })
// 导出
export const exportData = (type) => {
  uni.showLoading({ title: '生成中' })
  uni.downloadFile({
    url: `${BASE_URL}/export/${type}`,
    success: r => uni.openDocument({ filePath: r.tempFilePath, showMenu: true }),
    complete: () => uni.hideLoading()
  })
}